<?php
/**
 * Created by PhpStorm.
 * User: THINK
 * Date: 2017/8/30
 * Time: 11:29
 */
namespace Admin\Controller;
use Think\Controller;
class BlockController extends Controller{

    public function index(){

        $key = '';

        $where = '';

        if( I('keyword') ){

            $where = "a.name like '%".I('keyword')."%' ";

            $key = I('keyword');

        }

        $User = M('block');

        $count = $User->count();

        $Page = new \Think\Page($count,C('limitPage'));

        $show = $Page->show();

        $list = $User->alias('a')->field('a.id,a.name,a.blocktype,a.content,a.remark,b.name as names')->where("$where")->join("xyh_iteminfo as b on a.blocktype = b.value and b.group='blocktype'",'left')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('list',$list);

        $this->assign('key',$key);

        $this->assign('count',$count);

        $this->assign('page',$show);

        $this->display();

    }

    public function add(){

        $User = M('iteminfo');

        $list = $User->field('name,value')->where("`group` = 'blocktype'")->select();

        $this->assign('list',$list);

        $this->display();

    }

    public function addUser(){

        $data['name'] = I('name');

        $data['blocktype'] = I('blocktype');

        $data['content'] = I('content');

        $data['remark'] = I('remark');

        foreach ($data['content'] as $k => $v){

            if ($v!='') {

                $brr = $v;

            }

        }

        $data['content'] = $brr;

        $User = M('block');

        $User->add($data);

        $this->success('添加成功','index',3);

    }

    public function edit(){

        $id = I('id');

        $User = M('block');

        $list = $User->find($id);

        $Users = M('iteminfo');

        $lists = $Users->field('name,value')->where("`group` = 'blocktype'")->select();

        $this->assign('arr',$list);

        $this->assign('list',$lists);

        $this->display();

    }

    public function editUser(){

    }

    public function del(){

        $User = M('block');

        $id = is_array(I('id')) ? implode(',', I('id')) : I('id');

        if ( $User->delete($id) > 0 ){

            echo 1;

        }else{

            echo 0;

        }

    }

    public function upload(){

        $upload = new \Think\Upload();

        $upload->maxSize = 3145728 ;

        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');

        $upload->savePath  =  './Public/Uploads/';

        $info   =   $upload->upload();

        if(!$info) {

            $this->error($upload->getError());

        }else{

            $this->success('上传成功！');
        }

    }

    public function stype(){

        $this->display();

    }

}